package cn.edu.scau.cmi.yangjiamin.client;

import cn.edu.scau.cmi.yangjiamin.composite.safe.OrganizationComposite;
import cn.edu.scau.cmi.yangjiamin.composite.safe.PersonLeaf;
import cn.edu.scau.cmi.yangjiamin.composite.safe.OrganizationComponent;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class SafeCompositeGUI extends Application {

	private OrganizationComposite rootNode = new OrganizationComposite("华南农业大学", null);
	private OrganizationComponent nowNode;
	private List<OrganizationComponent> list = new CopyOnWriteArrayList<>();// 存储创建的OrganizationComponent对象

	@Override
	public void start(Stage primaryStage) throws Exception {
		list.add(rootNode);
		BorderPane pane = new BorderPane();
		BorderPane paneCenter = new BorderPane();

		VBox vbox = new VBox(15);
		vbox.setPadding(new Insets(5, 5, 5, 5));

		HBox hboxNode = new HBox(15);
		hboxNode.setPadding(new Insets(5, 5, 5, 5));
		Label labelNode = new Label("请先选择节点");
		ComboBox<String> cboNode = new ComboBox<>();
		cboNode.getItems().addAll(rootNode.getName());
		cboNode.setStyle("fx-color: red");
		hboxNode.getChildren().addAll(labelNode, cboNode);

		HBox hboxOp = new HBox(15);
		hboxOp.setPadding(new Insets(5, 5, 5, 5));
		Label labelOp = new Label("对节点进行操作");
		ComboBox<String> cboOp = new ComboBox<>();
		cboOp.getItems().addAll("创建子组织", "创建子成员", "删除子组织", "删除子成员", "展示父组织", "展示子组织或子成员");
		cboOp.setStyle("fx-color: red");
		hboxOp.getChildren().addAll(labelOp, cboOp);

		HBox hboxIn = new HBox(15);
		hboxIn.setPadding(new Insets(5, 5, 5, 5));
		Label labelIn = new Label("请输入组织或成员名:");
		TextField tfIn = new TextField();
		hboxIn.getChildren().addAll(labelIn, tfIn);

		vbox.getChildren().addAll(hboxNode, hboxOp, hboxIn);
		paneCenter.setCenter(vbox);
		pane.setTop(paneCenter);
		Text tShow = new Text(100, 100, "显示操作的结果");
		pane.setCenter(tShow);

		cboNode.setOnAction(eNode -> {
			String name = cboNode.getValue();
			nowNode = getObject(name);
			cboOp.setOnAction(eOp -> {
				tfIn.setText("");
				String operation = cboOp.getValue();
				if (operation.equals("创建子组织")) {
					if (nowNode instanceof OrganizationComposite) {
						tfIn.setOnAction(eTf -> {
							OrganizationComposite composite = new OrganizationComposite(tfIn.getText(),
									(OrganizationComposite) nowNode);
							
							if(((OrganizationComposite) nowNode).add(composite)) {
							list.add(composite);
							cboNode.getItems().add(composite.getName());
							tShow.setText(nowNode.getName() + " 创建子组织 " + composite.getName() + "成功");
							}else 
								tShow.setText(nowNode.getName() + " 已经有子组织 " + composite.getName());
						});
					} else {

						tShow.setText("成员" + nowNode.getName() + "无法创建子组织");
					}
				} else if (operation.equals("创建子成员")) {
					if (nowNode instanceof OrganizationComposite) {

						tfIn.setOnAction(eTf -> {
							PersonLeaf person = new PersonLeaf(tfIn.getText(), (OrganizationComposite) nowNode);
							if(((OrganizationComposite) nowNode).add(person)) {
							list.add(person);
							cboNode.getItems().add(person.getName());
							tShow.setText(nowNode.getName() + " 创建子成员 " + person.getName() + "成功");
							}else
								tShow.setText(nowNode.getName() + " 已经有子成员 " + person.getName());
						});

					} else {

						tShow.setText("成员" + nowNode.getName() + " 无法创建子成员");
					}

				} else if (operation.equals("删除子组织")) {// 处理层次为三层
					if (nowNode instanceof OrganizationComposite) {
						tfIn.setOnAction(eTf -> {
							OrganizationComponent composite = new OrganizationComposite();
							composite = getObject(tfIn.getText());
							if ( (composite != null) &&(composite instanceof  OrganizationComposite) 
									&& ((OrganizationComposite) nowNode).remove(composite)) {
								list.remove(composite);
								//System.out.println((((OrganizationComposite) nowNode).getChild()).size());
								cboNode.getItems().remove(composite.getName());// 节点选择框删除子组织
								List<OrganizationComponent> child = new CopyOnWriteArrayList<>();
								child = ((OrganizationComposite) composite).getChild();// 获得子组织的子组织
								//System.out.println(child.size());

								for (OrganizationComponent children : child) {
									//System.out.println(children.getName());
									list.remove(children);// 子组织的孩子节点也删除
									cboNode.getItems().remove(children.getName());
								}

								tShow.setText(nowNode.getName() + " 删除子组织 " + composite.getName() + "成功");
							} else
								tShow.setText(nowNode.getName() + " 没有子组织 " + tfIn.getText());
						});
					} else {
						tShow.setText("成员" + nowNode.getName() + "无子组织");
					}
				} else if (operation.equals("删除子成员")) {
					if (nowNode instanceof OrganizationComposite) {

						tfIn.setOnAction(eTf -> {
							PersonLeaf person = new PersonLeaf(tfIn.getText(), (OrganizationComposite) nowNode);
							
							if((person != null) && (person instanceof PersonLeaf) 
									&& ((OrganizationComposite) nowNode).remove(person)) {
							list.remove(person);
							cboNode.getItems().remove(person.getName());
							tShow.setText(nowNode.getName() + " 删除子成员 " + person.getName() + "成功");
							}else
								tShow.setText(nowNode.getName() + " 没有子成员 " + tfIn.getText());
						});

					} else {

						tShow.setText("成员" + nowNode.getName() + " 无子成员");
					}

				} else if (operation.equals("展示父组织")) {
					if (nowNode != rootNode) {
						tShow.setText(nowNode.getName() + "的父组织为: " + nowNode.getFather().getName());
					} else {
						tShow.setText(nowNode.getName() + "是根组织，无父组织");
					}
				} else {
					if (nowNode instanceof OrganizationComposite) {
						tShow.setText(
								nowNode.getName() + "的子组织或子成员为: " + "\n" + ((OrganizationComposite) nowNode).display());
					} else {
						tShow.setText(nowNode.getName() + " 没有子组织或子成员");
					}
				}
			});
		});

		Scene scene = new Scene(pane, 600, 500);
		primaryStage.setTitle("SafeComposite");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

	public OrganizationComponent getObject(String name) {// 从list获取对象，通过Name来判断
		for (OrganizationComponent oc : list) {
			if (oc.getName().equals(name)) {
				if (oc instanceof OrganizationComposite)
					return (OrganizationComposite) oc;
				else
					return (PersonLeaf) oc;
			}
		}
		return null;
	}

}
